﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotnet_lab4
{
    class GrowableArray : IEnumerable
    {

        private int[] fixedArray;
        private int[] growableArray;
        int freeIndex;

        public GrowableArray()
        {
            fixedArray = new int[10];

        }

        public GrowableArray(int size)
        {

            growableArray = new int[size];
            fixedArray = new int[10];

            for (int i = 0; i < size; i++)
            {
                growableArray[i] = i;
            }

        }

        public int this[int pos]
        {
            get { return growableArray[pos]; }
            set
            {
                if (fixedArray.Length > growableArray.Length)
                {

                    int[] doubleArray = new int[growableArray.Length * 2];

                    for (int i = 0; i < growableArray.Length; i++)
                    {
                        doubleArray[i] = growableArray[i];
                    }

                }
                else { growableArray[pos] = value; }

            }
        }

        public void Add(int i)
        {
            fixedArray[freeIndex] = i;
        }

        public IEnumerator GetEnumerator()
        {
            foreach (int i in fixedArray)
            {
                if (i == null)
                {
                    break;
                }
                yield return i;
            }
        }


        public String toString()
        {
            return "" + growableArray.Length;
        }
    }

}
